import re
from typing import List, Dict, Set

class BilingualFormatter:
    """双语内容格式化工具"""
    
    @staticmethod
    def format_content(content):
        """格式化双语内容"""
        sections = content.split("\n\n")
        formatted_sections = []
        
        for section in sections:
            if section.strip():
                # 分离英文和中文内容
                if "##" in section:
                    en_content, zh_content = section.split("##")
                    formatted_sections.append(f"{en_content.strip()}\n\n{zh_content.strip()}")
                else:
                    formatted_sections.append(section)
        
        return "\n\n".join(formatted_sections)

    @staticmethod
    def validate_citations(content):
        """验证引用格式"""
        citations = re.findall(r'<sup>(\d+)</sup>', content)
        return len(set(citations)) > 0

class TextProcessor:
    """文本处理工具"""
    
    @staticmethod
    def extract_citations(text: str) -> Set[str]:
        """提取引用标记"""
        return set(re.findall(r'<sup>(\d+)</sup>', text))
        
    @staticmethod
    def extract_references(text: str) -> List[Dict]:
        """提取参考文献"""
        refs = []
        pattern = r'\[(\d+)\]\s(.+?),\s(.+?),\s(\d{4})'
        for match in re.finditer(pattern, text):
            refs.append({
                'number': int(match.group(1)),
                'title': match.group(2).strip(),
                'venue': match.group(3).strip(),
                'year': int(match.group(4))
            })
        return refs
        
    @staticmethod
    def format_markdown(text: str) -> str:
        """格式化Markdown文本"""
        # 规范化标题
        text = re.sub(r'(?m)^(#+)\s*(.+?)$', r'\1 \2', text)
        # 规范化列表
        text = re.sub(r'(?m)^\*\s', '- ', text)
        # 规范化引用
        text = re.sub(r'\.\s*<sup>', '<sup>', text)
        return text.strip()
